package com.air.common.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.air.common.util.BasicAppConstant;
import com.air.common.util.BasicErrorCode;
import com.air.common.util.SubmitTokenFactory;
import com.air.common.util.WebUtil;
import com.air.common.vo.ResponseVO;
import com.air.common.vo.SubmitTokenVO;

public class DuplicateSubmitInterceptor extends HandlerInterceptorAdapter {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private List<String> validatorList;
	private List<String> issuerList;
	
	
	
	
	public void setValidatorList(List<String> validatorList) {
		this.validatorList = validatorList;
	}




	public void setIssuerList(List<String> issuerList) {
		this.issuerList = issuerList;
	}




	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, 
			Object handler)	throws Exception {
		

		boolean needIssue=false;
		boolean needValidate = false;
		boolean validateResult = true;
		String requestUrl = request.getRequestURL().toString();
		for (String pattern : issuerList){
			if (requestUrl.matches(pattern)){
				needIssue = true;
				break;
			}
		}
		
		for (String pattern : validatorList){
			if (requestUrl.matches(pattern)){
				needValidate = true;
				break;
			}
		}
		
		
		if (needValidate){
			validateResult= validateToken(request,response,handler);
		}
		if (!validateResult){
			return false;
		}
		
		if (needIssue){
			SubmitTokenVO token = SubmitTokenFactory.getInstance().applyNewToken(request);
			request.setAttribute(BasicAppConstant._KEY_SUBMIT_TOKEN, token);
			logger.info("ISSUE new SubmitToken "+JSONObject.fromObject(token));
		}
		
		
		
		return true;
	}
	
	
	
	
	private boolean validateToken(HttpServletRequest request, HttpServletResponse response, 
			Object handler)	throws Exception {
		
		boolean isAnsyCalling = WebUtil.isAnsyCalling(request);
		
		String issuer = request.getParameter(BasicAppConstant._PARA_SUBMIT_TOKEN_ISSUER)+"";
		String value = request.getParameter(BasicAppConstant._PARA_SUBMIT_TOKEN_VALUE)+"";
		
		
		boolean result=SubmitTokenFactory.getInstance().isValidToken(request, issuer, value);
		
		if (!result){
			logger.info("Validate Toke WRONG!! issue="+issuer+"  value="+value);
			ResponseVO responseVO = new ResponseVO();
			responseVO.setExecuteResult(false);
			responseVO.setErrCode(BasicErrorCode._ERRCODE_DULICATE_SUBMIT);
			request.setAttribute(BasicAppConstant._KEY_RESPONSE_VO, responseVO.toJsonFormat());
			
			if (isAnsyCalling){
				request.getRequestDispatcher(BasicAppConstant._URI_COMMON_JSON).forward(request, response);
			}else{
				request.getRequestDispatcher(BasicAppConstant._VIEW_COMMON_ERROR).forward(request, response);
			}
		}else{
			logger.info("Validate Toke OK!! issue="+issuer+"  value="+value);
		}
		return result;
	}
	
}
